<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Toast } from 'vant'
import api from '@/api'

const route = useRoute()
const router = useRouter()
const videoId = route.query.id

// 视频详情
const videoDetail = ref({
  id: videoId,
  title: '低空旅游体验：俯瞰千岛湖美景',
  author: '蓝天飞行员',
  avatar: 'https://picsum.photos/200/200?random=1',
  cover: 'https://pan.feiwow.com/raw/files/17b90398-8f34-4509-952d-79cb8dbdff07-1749241617399.png',
  views: 12543,
  likes: 1254,
  comments: 324,
  publishTime: '2023-10-15 12:30:45',
  duration: '03:45',
  description: '这是一段关于千岛湖低空旅游的精彩视频，从空中俯瞰千岛湖的壮丽景色，领略大自然的鬼斧神工。视频包含了飞行过程中的精彩瞬间，以及飞行员的专业解说，带您领略不一样的旅游体验。',
  tags: ['低空旅游', '千岛湖', '航空体验']
})

// 评论列表
const comments = reactive([
  {
    id: 1,
    username: '云端漫步者',
    avatar: 'https://picsum.photos/200/200?random=2',
    content: '视频拍得太棒了，真想亲自去体验一次！',
    time: '2023-10-15 13:45',
    likes: 45
  },
  {
    id: 2,
    username: '航空爱好者',
    avatar: 'https://picsum.photos/200/200?random=3',
    content: '请问这个项目大概需要多少费用？有没有什么特殊要求？',
    time: '2023-10-15 14:20',
    likes: 23
  },
  {
    id: 3,
    username: '旅行达人',
    avatar: 'https://picsum.photos/200/200?random=4',
    content: '千岛湖确实是个好地方，从空中看更壮观！期待更多类似的视频。',
    time: '2023-10-15 15:10',
    likes: 18
  }
])

// 相关视频
const relatedVideos = reactive([
  {
    id: 101,
    title: '杭州西湖低空游览体验',
    cover: 'https://pan.feiwow.com/raw/files/9f8e76e6-3aed-41e0-bb68-ac8c995abfbe-1749241924810.png',
    views: 8976,
    duration: '04:32'
  },
  {
    id: 102,
    title: '飞行员视角：云海穿越',
    cover: 'https://pan.feiwow.com/raw/files/0918b246-4f79-4516-b300-dd70ddbb7834-1749241488302.jpeg',
    views: 6543,
    duration: '02:45'
  },
  {
    id: 103,
    title: '低空旅游安全须知',
    cover: 'https://pan.feiwow.com/raw/files/17b90398-8f34-4509-952d-79cb8dbdff07-1749241617399.png',
    views: 4321,
    duration: '05:18'
  }
])

// 是否已点赞
const isLiked = ref(false)
// 是否已收藏
const isCollected = ref(false)
// 评论内容
const commentContent = ref('')
// 是否显示评论框
const showCommentInput = ref(false)

// 页面加载完成后初始化
onMounted(() => {
  loadVideoDetail()
  loadComments()
  loadRelatedVideos()
})

// 加载视频详情
const loadVideoDetail = async () => {
  try {
    // 实际项目中应该从API获取数据
    // const response = await api.video.getVideoDetail(videoId)
    // videoDetail.value = response.data
    console.log('加载视频详情:', videoId)
  } catch (error) {
    console.error('加载视频详情失败:', error)
    Toast('加载视频详情失败')
  }
}

// 加载评论列表
const loadComments = async () => {
  try {
    // 实际项目中应该从API获取数据
    // const response = await api.video.getComments(videoId)
    // comments.value = response.data
    console.log('加载评论列表')
  } catch (error) {
    console.error('加载评论列表失败:', error)
  }
}

// 加载相关视频
const loadRelatedVideos = async () => {
  try {
    // 实际项目中应该从API获取数据
    // const response = await api.video.getRelatedVideos(videoId)
    // relatedVideos.value = response.data
    console.log('加载相关视频')
  } catch (error) {
    console.error('加载相关视频失败:', error)
  }
}

// 处理点赞
const handleLike = () => {
  if (isLiked.value) {
    videoDetail.value.likes--
  } else {
    videoDetail.value.likes++
  }
  isLiked.value = !isLiked.value
  Toast(isLiked.value ? '点赞成功' : '已取消点赞')
}

// 处理收藏
const handleCollect = () => {
  isCollected.value = !isCollected.value
  Toast(isCollected.value ? '收藏成功' : '已取消收藏')
}

// 处理分享
const handleShare = () => {
  Toast('分享功能开发中')
}

// 处理评论提交
const submitComment = () => {
  if (!commentContent.value.trim()) {
    Toast('评论内容不能为空')
    return
  }
  
  // 实际项目中应该调用API
  // await api.video.submitComment(videoId, commentContent.value)
  
  // 模拟添加评论
  const newComment = {
    id: comments.length + 1,
    username: '当前用户',
    avatar: 'https://picsum.photos/200/200?random=10',
    content: commentContent.value,
    time: '刚刚',
    likes: 0
  }
  
  comments.unshift(newComment)
  commentContent.value = ''
  showCommentInput.value = false
  Toast('评论成功')
}

// 处理相关视频点击
const handleRelatedVideoClick = (video) => {
  router.push({
    path: '/video-detail',
    query: { id: video.id }
  })
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 格式化观看次数
const formatViews = (views) => {
  if (views < 1000) {
    return views.toString()
  } else if (views < 10000) {
    return (views / 1000).toFixed(1) + 'k'
  } else {
    return (views / 10000).toFixed(1) + 'w'
  }
}
</script>

<template>
  <div class="page-container">
    <!-- 顶部导航栏 -->
    <div class="navbar">
      <div class="back-btn" @click="goBack">
        <van-icon name="arrow-left" />
      </div>
      <div class="nav-title">视频详情</div>
      <div class="placeholder"></div>
    </div>
    
    <!-- 视频播放器 -->
    <div class="video-player">
      <img :src="videoDetail.cover" :alt="videoDetail.title" class="video-cover">
      <div class="play-btn">
        <van-icon name="play-circle-o" />
      </div>
    </div>
    
    <!-- 视频信息 -->
    <div class="video-info">
      <div class="video-title">{{ videoDetail.title }}</div>
      <div class="video-stats">
        <span>{{ formatViews(videoDetail.views) }}次观看</span>
        <span>{{ videoDetail.publishTime }}</span>
      </div>
      
      <!-- 作者信息 -->
      <div class="author-info">
        <img :src="videoDetail.avatar" :alt="videoDetail.author" class="author-avatar">
        <div class="author-name">{{ videoDetail.author }}</div>
        <van-button type="primary" size="small" class="follow-btn">关注</van-button>
      </div>
      
      <!-- 操作按钮 -->
      <div class="action-buttons">
        <div class="action-btn" @click="handleLike">
          <van-icon :name="isLiked ? 'like' : 'like-o'" :class="{ active: isLiked }" />
          <span>{{ videoDetail.likes }}</span>
        </div>
        <div class="action-btn" @click="() => showCommentInput = true">
          <van-icon name="comment-o" />
          <span>{{ videoDetail.comments }}</span>
        </div>
        <div class="action-btn" @click="handleCollect">
          <van-icon :name="isCollected ? 'star' : 'star-o'" :class="{ active: isCollected }" />
          <span>收藏</span>
        </div>
        <div class="action-btn" @click="handleShare">
          <van-icon name="share-o" />
          <span>分享</span>
        </div>
      </div>
      
      <!-- 视频描述 -->
      <div class="video-description">
        <div class="description-content">{{ videoDetail.description }}</div>
        <div class="video-tags">
          <span v-for="(tag, index) in videoDetail.tags" :key="index" class="tag">
            #{{ tag }}
          </span>
        </div>
      </div>
    </div>
    
    <!-- 评论区 -->
    <div class="comments-section">
      <div class="section-title">评论 ({{ comments.length }})</div>
      <div class="comments-list">
        <div v-for="comment in comments" :key="comment.id" class="comment-item">
          <img :src="comment.avatar" :alt="comment.username" class="comment-avatar">
          <div class="comment-content">
            <div class="comment-header">
              <div class="comment-username">{{ comment.username }}</div>
              <div class="comment-time">{{ comment.time }}</div>
            </div>
            <div class="comment-text">{{ comment.content }}</div>
            <div class="comment-actions">
              <div class="comment-like">
                <van-icon name="like-o" />
                <span>{{ comment.likes }}</span>
              </div>
              <div class="comment-reply">回复</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 相关视频 -->
    <div class="related-videos">
      <div class="section-title">相关视频</div>
      <div class="related-list">
        <div 
          v-for="video in relatedVideos" 
          :key="video.id" 
          class="related-item"
          @click="handleRelatedVideoClick(video)"
        >
          <div class="related-cover">
            <img :src="video.cover" :alt="video.title">
            <div class="related-duration">{{ video.duration }}</div>
          </div>
          <div class="related-info">
            <div class="related-title">{{ video.title }}</div>
            <div class="related-views">{{ formatViews(video.views) }}次观看</div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 评论输入框弹窗 -->
    <van-popup
      v-model:show="showCommentInput"
      position="bottom"
      :style="{ height: '40%' }"
      round
    >
      <div class="comment-popup">
        <div class="popup-header">
          <div class="popup-title">发表评论</div>
          <van-icon name="cross" @click="showCommentInput = false" />
        </div>
        <div class="comment-textarea">
          <textarea 
            v-model="commentContent" 
            placeholder="说点什么吧..."
            rows="4"
          ></textarea>
        </div>
        <div class="comment-submit">
          <van-button type="primary" block @click="submitComment">发表评论</van-button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<style scoped>
.page-container {
  background-color: white;
  min-height: 100vh;
}

/* 顶部导航栏 */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background-color: white;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 30px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.back-btn .van-icon {
  font-size: 20px;
}

.nav-title {
  font-size: 16px;
  font-weight: bold;
}

.placeholder {
  width: 30px;
}

/* 视频播放器 */
.video-player {
  width: 100%;
  position: relative;
  padding-top: 56.25%; /* 16:9 宽高比 */
  background-color: black;
}

.video-cover {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.play-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-size: 60px;
  opacity: 0.8;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

/* 视频信息 */
.video-info {
  padding: 15px;
  border-bottom: 8px solid #f5f7fa;
}

.video-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 8px;
  line-height: 1.4;
}

.video-stats {
  display: flex;
  color: #999;
  font-size: 12px;
  margin-bottom: 15px;
}

.video-stats span:not(:last-child) {
  margin-right: 15px;
}

/* 作者信息 */
.author-info {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.author-avatar {
  width: 40px;
  height: 40px;
  border-radius: 20px;
  margin-right: 10px;
}

.author-name {
  flex: 1;
  font-size: 14px;
  font-weight: bold;
}

.follow-btn {
  flex-shrink: 0;
  border-radius: 15px;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 15px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #666;
}

.action-btn .van-icon {
  font-size: 20px;
  margin-bottom: 5px;
}

.action-btn span {
  font-size: 12px;
}

.action-btn .van-icon.active {
  color: var(--primary-color);
}

/* 视频描述 */
.video-description {
  margin-top: 15px;
}

.description-content {
  font-size: 14px;
  line-height: 1.6;
  color: #333;
}

.video-tags {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
}

.tag {
  background-color: #f5f7fa;
  color: var(--primary-color);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-right: 8px;
  margin-bottom: 8px;
}

/* 评论区 */
.comments-section {
  padding: 15px;
  border-bottom: 8px solid #f5f7fa;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
}

.comments-list {
  background-color: white;
}

.comment-item {
  display: flex;
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.comment-item:last-child {
  border-bottom: none;
}

.comment-avatar {
  width: 40px;
  height: 40px;
  border-radius: 20px;
  margin-right: 10px;
}

.comment-content {
  flex: 1;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
}

.comment-username {
  font-size: 14px;
  font-weight: bold;
}

.comment-time {
  font-size: 12px;
  color: #999;
}

.comment-text {
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 8px;
}

.comment-actions {
  display: flex;
  font-size: 12px;
  color: #999;
}

.comment-like {
  display: flex;
  align-items: center;
  margin-right: 15px;
}

.comment-like .van-icon {
  margin-right: 4px;
}

.comment-reply {
  color: #999;
}

/* 相关视频 */
.related-videos {
  padding: 15px;
}

.related-list {
  background-color: white;
}

.related-item {
  display: flex;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.related-item:last-child {
  border-bottom: none;
}

.related-cover {
  width: 120px;
  height: 70px;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  margin-right: 10px;
  flex-shrink: 0;
}

.related-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.related-duration {
  position: absolute;
  bottom: 5px;
  right: 5px;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 1px 4px;
  border-radius: 2px;
  font-size: 10px;
}

.related-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.related-title {
  font-size: 14px;
  font-weight: bold;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.related-views {
  font-size: 12px;
  color: #999;
}

/* 评论弹窗 */
.comment-popup {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 15px;
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.popup-title {
  font-size: 16px;
  font-weight: bold;
}

.comment-textarea {
  flex: 1;
  margin-bottom: 15px;
}

.comment-textarea textarea {
  width: 100%;
  height: 100%;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  resize: none;
}

.comment-submit {
  margin-top: auto;
}
</style>